package com.gitee.l0km.codegen.generic;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.gitee.l0km.codegen.annotations.DefaultGenericTypes;
import com.gitee.l0km.codegen.base.CodeGenUtils;
import com.gitee.l0km.com4j.base.Assert;

import java.util.Set;

public class DefaultParameterGenericTypes  implements GenericInterfaceConstants{
	private final Map<Class<?>,String> defaultGenericTypes;
	DefaultParameterGenericTypes(Class<?>clazz) {
		Assert.notNull(clazz,"clazz");
		defaultGenericTypes=getDefaultParameterGenericTypes(clazz);
	}
	private final Map<Class<?>,String> getDefaultParameterGenericTypes(Class<?>clazz) {
		DefaultGenericTypes def = clazz.getAnnotation(DefaultGenericTypes.class);
		try {
			return null != def ? CodeGenUtils.createMap(def.types(), def.names(), true) : GENERIC_TYPES_DEFAULT;
		} catch (IllegalArgumentException e) {
			throw new DefaultGenericTypesExceptoin(e);
		}
	}
	/**
	 * @param clazz
	 * @see java.util.HashMap#get(java.lang.Object)
	 */
	public String getName(Class<?> clazz) {
		return defaultGenericTypes.get(clazz);
	}
	/**
	 * @param name
	 * @see java.util.HashMap#containsKey(java.lang.Object)
	 */
	public boolean hasName(String name) {
		return defaultGenericTypes.containsKey(name);
	}
	/**
	 * @param clazz
	 * @see java.util.HashMap#containsValue(java.lang.Object)
	 */
	public boolean hasType(Class<?> clazz) {
		return defaultGenericTypes.containsKey(clazz);
	}

	/**
	 * @see java.util.HashMap#keySet()
	 */
	public Set<String> names() {
		return new HashSet<String>(defaultGenericTypes.values());
	}
	@Override
	public String toString() {
		StringBuilder builder=new StringBuilder();
		Iterator<Entry<Class<?>, String>> it = defaultGenericTypes.entrySet().iterator();
		Entry<Class<?>, String> entry;
		builder.append("{\n");
		while(it.hasNext()){
			entry = it.next();
			builder.append(entry.getKey().getName()).append(',').append(entry.getValue()).append("\n");
		}
		builder.append("}\n");
		return builder.toString();
	}
}
